app.controller('goodsTemplateController' ,function($scope,$controller   ,goodsTemplateService,goodsBrandService,goodsSpecificationService){

    $controller('commonController',{$scope:$scope});

    //读取列表数据绑定到表单中
    $scope.findAll=function(){
        goodsTemplateService.findAll().success(
            function(response){
                $scope.list=response;
            }
        );
    }

    //分页
    $scope.findPage=function(pageNum,pageSize){
        goodsTemplateService.findPage(pageNum,pageSize).success(
            function(response){
                $scope.list=response.result;
                $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }

    //查询实体
    $scope.findOne=function(id){
        goodsTemplateService.findOne(id).success(
            function(response){
                $scope.entity= response;
                //转换字符串为json对象（集合）
                $scope.entity.brandIds=  JSON.parse( $scope.entity.brandIds);
                $scope.entity.specIds= JSON.parse($scope.entity.specIds);
                $scope.entity.customAttributeItems = JSON.parse($scope.entity.customAttributeItems);

            }
        );
    }

    //保存
    $scope.save=function(){
        var serviceObject;//服务层对象
        if($scope.entity.id!=null){//如果有ID
            serviceObject=goodsTemplateService.update( $scope.entity ); //修改
        }else{
            serviceObject=goodsTemplateService.add( $scope.entity  );//增加
        }
        serviceObject.success(
            function(response){
                if(200==response.code){
                    //重新查询
                    $scope.reloadList();//重新加载
                }else{
                    alert(response.message);
                }
            }
        );
    }


    //批量删除
    $scope.delete=function(){
        //获取选中的复选框
        goodsTemplateService.delete( $scope.selectIds ).success(
            function(response){
                if(200==response.code){
                    $scope.reloadList();//刷新列表
                    $scope.selected=[];
                }
            }
        );
    }

    $scope.searchEntity={};//定义搜索对象

    //搜索
    $scope.search=function(pageNum,pageSize){
        goodsTemplateService.search(pageNum,pageSize,$scope.searchEntity).success(
            function(response){
                $scope.list=response.result;
                $scope.paginationConf.totalItems=response.total;//更新总记录数
            }
        );
    }

    //品牌列表
    $scope.brandList={data:[]};
    //读取品牌列表
    $scope.findBrandList=function(){
        goodsBrandService.selectBrandOption().success(
            function(response){
                $scope.brandList={data:response};
            }
        );
    }

    //规格列表
    $scope.specList={data:[]};
    //读取规格列表
    $scope.findSpecList=function(){
        goodsSpecificationService.selectSpecList().success(
            function(response){
                $scope.specList={data:response};
            }
        );
    }

    //增加扩展属性行
    $scope.addTableRow=function(){
        $scope.entity.customAttributeItems.push({});
    }
    //删除扩展属性行
    $scope.deleteTableRow=function(index){
        $scope.entity.customAttributeItems.splice( index,1);
    }

});
